package handler

import (
	"github.com/gin-gonic/gin"
	"short-vid-hub/model"
	"strconv"
)

func AddVid(c *gin.Context) {
	title := c.PostForm("title")
	author := c.PostForm("author")
	like_num := c.PostForm("like_num")
	time := c.PostForm("time")
	commit := c.PostForm("commit")
	if title == "" || author == "" || like_num == "" || time == "" || commit == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不可为空",
		})
		return
	}
	data := model.Vid{
		Title:   title,
		Author:  author,
		LikeNum: like_num,
		Time:    time,
		Commit:  commit,
	}
	model.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
	})
	return
}
func VidList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))
	offset := (page - 1) * size
	var vid []model.Vid
	model.DB.Offset(offset).Limit(size).Debug().Find(&vid)
	var total int64
	model.DB.Model(&model.Vid{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "OK",
		"data":  vid,
		"total": total,
	})
}
func VidDetail(c *gin.Context) {
	id := c.Query("id")
	var vid model.Vid
	model.DB.Where("id=?", id).Debug().Find(&vid)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "OK",
		"data": vid,
	})
}
